package com.ctsi.ssdc.service.impl.front;

import com.ctsi.ssdc.mapper.IndustrialIndicatorsMapper1;
import com.ctsi.ssdc.service.front.IndustrialIndicatorsService1;
import com.ctsi.ssdc.entity.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.*;

@Service
public class IndustrialIndicatorsService1Impl implements IndustrialIndicatorsService1 {
    @Autowired
    IndustrialIndicatorsMapper1 industrialIndicatorsMapper1;

    /**
     * 分区镇规上工业总产值
     *
     * @param year  年份
     * @param month 月份
     * @return List<SubDistrictTownIndustry>
     */
    @Override
    public List<SubDistrictTownIndustry> getSubDistrictTownIndustry(int year, int month) {
        return industrialIndicatorsMapper1.findSubDistrictTownIndustry(year, month);
    }

    /**
     * 工业总产值
     * 规上工业总产值
     * 综合能源消费量（吨标准煤）
     * 规上工业用电量（万千瓦时）
     * 万元产值能耗（吨标准煤/万元）
     *
     * @param year       年份
     * @param indicators 指标
     * @return List<IndustrialEconomicIndicators>
     */
    @Override
    public List<IndustrialEconomicIndicators> getIndustrialIndicatorsByIndicators(int year, String indicators) {
        return industrialIndicatorsMapper1.findIndustrialIndicatorsByIndicators(year, indicators);
    }

    /**
     * 高新技术产业产值
     *
     * @param year 年份
     * @return List<HighTechIndustry>
     */
    @Override
    public List<HighTechIndustry> getHighTechIndustry(int year) {
        return industrialIndicatorsMapper1.findHighTechIndustry(year);
    }

    /**
     * 头标：当月及当月累计
     *
     * @param year 年份，month 月份, indicators 指标
     * @return IndustrialEconomicIndicators
     */
    @Override
    public IndustrialEconomicIndicators getMonthProduct(int year, int month, String indicators) {
        return industrialIndicatorsMapper1.findMonthProduct(year, month, indicators);
    }

    /**
     * 规模以上工业分行业产值
     *
     * @param year 年份，month 月份
     * @return List<BasicIndustry>
     */
    @Override
    public List<SectorProduct> getSectorProduct(int year, int month) {
        List<SectorProduct> list = industrialIndicatorsMapper1.findSectorProduct(year, month);
        Iterator<SectorProduct> it = list.iterator();
        int i = 0;
        while (it.hasNext()) {
            SectorProduct sectorProduct = it.next();
            if (i == 0) {
                sectorProduct.setSectorName("总计");
            }
            i++;
            sectorProduct.setNumber(i);
            if (sectorProduct.getPreviousAccumulate() == 0) {
                continue;
            } else {
                double monthOnMonthAccumulate = (sectorProduct.getCurrentAccumulate() - sectorProduct.getPreviousAccumulate()) * 100 / sectorProduct.getPreviousAccumulate();
                double value = new BigDecimal(monthOnMonthAccumulate).setScale(1, BigDecimal.ROUND_HALF_UP).doubleValue();
                sectorProduct.setMonthOnMothAccumulate(value);
            }
        }
        return list;
    }

}
